package me.jiatao.javestudy.generic.type_variable;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

/**
 * Created by JiaTao on 2018/9/12.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */
public class TypeVariableMain {
    public static void main(String[] args) {

        Class<?> clazz = PointGenericityImpl.class;
        Type[] types = clazz.getGenericInterfaces();
        for (Type type : types) {
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                //返回表示此类型实际类型参数的 Type 对象的数组
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                for (Type parameterArgType : actualTypeArguments) {
                    if (parameterArgType instanceof TypeVariable) {
                        TypeVariable typeVariable = (TypeVariable) parameterArgType;
                        System.out.println("此接口的填充类型为：" + typeVariable.getName());

                        //返回表示此类型变量上边界的 Type 对象的数组。
                        Type[] typebounds = typeVariable.getBounds();
                        for (Type bound : typebounds) {
                            Class<?> boundClass = (Class) bound;
                            //如果不写，则默认输出Object，如果写了，则输出对应的
                            System.out.println("bound为：" + boundClass.getName());
                        }
                    }
                    if (parameterArgType instanceof Class) {
                        Class parameterArgClass = (Class) parameterArgType;
                        System.out.println("此接口的填充类型为：" + parameterArgClass.getName());
                    }
                }

            }
        }
    }
}
